<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Notice;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class NoticeController extends Controller
{
    /*
     * 公告列表
     */
    public function index(Request $request, Notice $notice)
    {
        if (isset($request->condition) && !empty($request->condition)) {
            $res = $notice->indexPageLike($request->page, $request->limit, $request->condition);
        } else {
            $res = $notice->indexPage($request->page, $request->limit);
        }
        if ($res && count($res) > 0) {
            return $this->return_json(200, $res, '查询成功');
        } else {
            return $this->return_json(201, '', '查询失败');
        }
    }

    /*
     * 公告记录数
     */
    public function indexNum(Request $request, Notice $notice)
    {
        if (isset($request->condition) && !empty($request->condition)) {
            $condition = $request->condition;
//          $res = $notice->where('title','like',"%$request->condition%")->where('status',1)->count();
            $query = 'SELECT COUNT(*) as count FROM `notice` WHERE `status` = 1 and CONCAT(`title`,`content`,`admin_name`,`admin_position`,`created_at`) LIKE ?';
            $param = '%' . $condition . '%';
            $count = DB::select($query, array($param));
            $res = $count[0]->count;
        } else {
            $res = $notice->where('status', 1)->count();
        }
        if ($res) {
            return $this->return_json(200, $res, '查询成功');
        } else {
            return $this->return_json(201, '', '查询失败');
        }
    }

    /*
     * 创建公告
     */
    public function save(Request $request, Notice $notice)
    {
        $param = $request->all();
        //先固定,后期查询角色表获取
        $param['admin_name'] = '楚留香';
        $param['admin_position'] = '物业主管';
        $code = $notice->create($param);
        if ($code) {
            return $this->return_json(200, '', '添加成功');
        } else {
            return $this->return_json(201, '', '添加失败');
        }
    }

    /*
     * 修改公告
     */
    public function update(Request $request, Notice $notice)
    {
        $code = $notice->whereid($request->id)->update($request->all());
        if ($code) {
            return $this->return_json(200, '', '修改成功');
        } else {
            return $this->return_json(201, '', '修改失败');
        }
    }

    /*
     * 删除公告
     */
    public function delete(Request $request, Notice $notice)
    {
        $param = $request->all();
        $param['status'] = 0;
        $code = $notice->whereid($request->id)->update($param);
        if ($code) {
            return $this->return_json(200, '', '删除成功');
        } else {
            return $this->return_json(201, '', '删除失败');
        }
    }

    /*
     * 根据公告id获取公告
     */
    public function getNoticeById(Request $request, Notice $notice)
    {

        $res = $notice->whereid($request->id)->get();
        if ($res && !$res->isEmpty()) {
            return $this->return_json(200, $res, '查询成功');
        } else {
            return $this->return_json(201, '', '查询失败');
        }
    }
}
